using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;

namespace Kaleida.ServiceMonitor.Model.Email
{
    internal class BasicTable
    {
        public class Row
        {
            private readonly List<IHtmlString> items;

            public Row(IEnumerable<IHtmlString> items)
            {
                this.items = items.ToList();
            }

            public List<IHtmlString> Items
            {
                get { return items; }
            }
        }

        private List<IHtmlString> headings = new List<IHtmlString>();
        private readonly List<Row> rows = new List<Row>();
       
        public void AddHeaderRow(IList<string> values)
        {
            AddHeaderRow(values.Select(i => i.HtmlEncode()).ToList());
        }

        public void AddHeaderRow(IList<IHtmlString> values)
        {
            if (rows.Any())
                throw new InvalidOperationException("Must add Header row before Content");

            headings = values.ToList();
        }

        public void AddRow(IList<string> values)
        {
            AddRow(values.Select(i => i.HtmlEncode()).ToList());
        }

        public void AddRow(IList<IHtmlString> values)
        {
            if (values.Count() != headings.Count())
                throw new InvalidOperationException(string.Format("Expected {0} value(s) but received {1}", headings.Count(), values.Count()));

            rows.Add(new Row(values));
        }

        public IList<IHtmlString> Headings
        {
            get { return headings.AsReadOnly(); }
        }

        public IList<Row> Rows
        {
            get { return rows.AsReadOnly(); }
        }
    }
}